package com.xxx.controller;

import com.xxx.controller.utils.R;
import com.xxx.pojo.Order;
import com.xxx.pojo.OrderItem;
import com.xxx.service.CartService;
import com.xxx.service.OrderItemService;
import com.xxx.service.OrderService;
import com.xxx.service.utils.MD5Util;
import com.xxx.vo.CartVo;
import com.xxx.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private CartService cartService;

    //创建订单功能实现
    @RequestMapping("/createOrder")
    public R createOrder(String data, HttpSession session,String totalPrice,String cidStr){
        //保存总订单
        Order order=new Order();
        //集合存取 : 后面的有用数据
        List use =new ArrayList();
        //接收前端的数据 进行截取
        String[] list = data.split(",");
        for (String s : list) {
            String[] result = s.split(":");
            use.add(result[1]);
        }
        //赋值给order.
        //MD5设置订单编号
        String orderNum=MD5Util.getMD5(new Date()+"");
        order.setOrderNum(orderNum);
        order.setUid(getUidFromSession(session));
        order.setRecvName(use.get(0).toString());
        order.setRecvTag(use.get(1).toString());
        order.setRecvAddress(use.get(2).toString());
        order.setRecvPhone(use.get(3).toString());
        order.setTotalPrice(totalPrice);
        orderService.creatOrder(order,getUsernameFromSession(session));

        //保存单个订单详细信息
        String[] split = cidStr.split("&");
        Integer[] cids=new Integer[split.length];
        for(int i=0;i<split.length;i++){
             cids[i]=Integer.parseInt(split[i].split("=")[1]);
        }
        //存数据到orderItem中
        List<CartVo> useful = cartService.getOrderList(cids);
        for(int i=0;i<useful.size();i++){
            //通过cid 获取所需要的title image price num
                OrderItem orderItem = new OrderItem();
                orderItem.setOrderNum(orderNum);
                orderItem.setUid(getUidFromSession(session));
                orderItem.setTitle(useful.get(i).getTitle());
                orderItem.setImage(useful.get(i).getImage());
                orderItem.setPrice(useful.get(i).getPrice());
                orderItem.setNum(useful.get(i).getNum());
                orderItem.setStatus(0);
                orderItemService.saveOrderItem(orderItem, getUsernameFromSession(session));
        }
        //返回订单编号
        String[] num={orderNum};
        return new R(OK,num);
    }

    //显示全部订单 或者部分订单信息功能

    @RequestMapping("/getOrderList")
    public R getOrderList(HttpSession session,Integer status){
        List<List<OrderItemVo>> orderItemVoList = orderService.getOrderItemVoList(getUidFromSession(session),status);
        return new R(OK,orderItemVoList);
    }

    //显示单个订单信息功能
    @RequestMapping("/getOneOrder")
    public R getOneOrder(String orderNum,Integer id){
        OrderItemVo oneOrderItemVo = orderService.getOneOrderItemVo(orderNum, id);
        return new R(OK,oneOrderItemVo);
    }

    //确认收货功能实现
    @RequestMapping("/confirmOrder")
    public R confirmOrder(Integer[] ids){
        orderService.confirm(ids);
        return new R(OK);
    }

//    //通过用户名uid  状态 status查询 待付款 待收货 待评价 退货退款的订单列表
//    @RequestMapping("/getOrderList")
//    public R getOrderList(HttpSession session,Integer status){
//        List<OrderItemVo> orderList = orderService.getOrderList(getUidFromSession(session), status);
//        return new R(OK,orderList);
//    }
}
